package org.artifact.core.context.session;

import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;

public class SessionManager {
    /** 所有链接服务器的会话. */
    private static final ConcurrentMap<Serializable, Session> SESSIONS = new ConcurrentHashMap<>(2048);

    public static Session createSession(Serializable id, Function<Serializable, Session> mappingFunction) {
        return SESSIONS.computeIfAbsent(id, mappingFunction);
    }

    /**
	 * 根据链接ID来获取Session.
	 * 
	 * @param id 链接ID
	 * @return Session对象
	 */
	public static Session getSession(Serializable id) {
		return SESSIONS.get(id);
	}
	
	/**
	 * 当客户端断开后清理Session的方法.
	 * 
	 * @param session Session对象
	 */
	public static void removeSession(Session session) {
		SESSIONS.remove(session.getId());
	}
}
